<?php

require_once($INCLUDE_PATH.'app/controller/InitController.php');
require_once ($INCLUDE_PATH . "/inc/checkfun.php");
require_once ($INCLUDE_PATH . "/include/WebService2Db.php");

function __autoload($class_name) {
    require_once APP_ROOT.'app/model/'.strtolower($class_name) . '.php';
}
class IndexController extends InitController {

    public function __construct() {
        parent::__construct();

    }

    public function actionMain() {
        $server = IndexModel::getServerInfo();
        Y::$tpl->set("server",$server);
    }
    public function actionTop() {
        Y::$tpl->set("adminname",$_COOKIE['adminname']);
        $user_ip = DyAuth::getip();
        Y::$tpl->set("user_ip",$user_ip);
    }
    public function actionDefault() {


    }

    public function actionLogin() {

        if(!empty($_REQUEST['username'])) {

            $username=$_REQUEST['username'];
            $password=$_REQUEST['password'];

            // $yzm=trim($_POST['yzm']);
            if(!CheckFun::checkusername($username)) {
                return self::sysMessage("温馨提示！您输入的用户名错误！", array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
            }
            if(!CheckFun::checkpassword($password)) {
                return self::sysMessage("温馨提示！您输入的密码错误！", array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
            }
     /*   if(!preg_match("/\A[A-Za-z0-9]{4}\Z/",$yzm)) {
            $sql="insert into {$pre}adminlogo (uid,uname,logintime,lasttime,ip,adminlevel,remark) " .
                "VALUES (0,'{$username}',{$now},{$now},'{$ip}',0,'��֤���ʽ����')";
            $conn->query($sql);
            die("<script LANGUAGE='javascript'>alert('��ܰ��ʾ�����������֤�����');history.go(-1);</script>");
        }
        $cookieyzm=$_COOKIE['verifycode'];
        setcookie("verifycode","",time()+10*60);
        if(strcasecmp($yzm,$cookieyzm)!=0) {
            $sql="insert into {$pre}adminlogo (uid,uname,logintime,lasttime,ip,adminlevel,remark) " .
                "VALUES (0,'{$username}',{$now},{$now},'{$ip}',0,'��֤���������')";
            $conn->query($sql);
            die("<script LANGUAGE='javascript'>alert('��ܰ��ʾ����������ȷ����֤�룡');history.go(-1);</script>");
        }
*/;

            $password=md5($password);
            $query = IndexModel::selectuser($username,$password);
            if($query) {

            // $query=query_first($sql);
                $res =  IndexModel::updatauser($username,$password,$query);
                if ($_REQUEST['j'] == "do") {
                    echo '{"key":"'.$res[3].'"}';
                    Y::$htmlFile = 'index/empty.html';
                }else {
                    header("location:index.php");
                }
            }else {
                return self::sysMessage("登陆失败！", array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
            }
        }
    }
    /**
     * parse category to group
     */
    private function parseCategory($categorys,$child) {
        $num =3;
        foreach($categorys as $category) {
            $category['child']=$child['cvshop_category_child_'.$category['id']];
            $group[]=$category;
            if( $num == count($group)) {
                $all[]=$group;
                $group=array();
            }
        }
        if( 0 < count($group) ) {
            $all[]=$group;
        }

        Y::$tpl->set('categorys',$all);
    }

    /**
     * parse yicunyipin  geo info
     */

    public function actionLogout() {
        setcookie("adminname","",0);
        setcookie("adminpassword","",0);
        setcookie("usertype","",0);
        header("location:index.php");
    }


}

?>
